# 人生苦短，我用python
# @Time : 2021/10/12 10:13
# @Author : shiChao
# @File : excelRequest.py
# @Desc : 操作Excel用例的方法
import json
from apiunittest.lib.loadIni import LoadIni


class ExcelRequest:
    """发送请求数据"""
    @staticmethod
    def requests(s, apiData: dict) -> object:
        """

        :rtype: object
        """
        # 域名配置，分测试、预发布、线上
        # domain = LoadIni('config.ini').getConfig('domain', 'domain')

        try:
            # 从读取的表格中获取响应的参数作为传递
            method = apiData["method"]
            # url = domain + apiData["URL"]
            url = apiData["URL"]

            # 请求参数
            if apiData["params"] == "":
                par = None
            else:
                par = eval(apiData["params"])

            # header头
            if apiData["headers"] == "":
                h = None
            else:
                h = eval(apiData["headers"])

            # body体
            if apiData["body"] == "":
                body_data = None
            else:
                body_data = eval(apiData["body"])
            requestType = apiData["type"]
            v = False
            if requestType == "data":
                body = body_data
            elif requestType == "json":
                body = json.dumps(body_data)
            else:
                body = body_data

            # 发送请求
            re = s.request(method=method, url=url, headers=h, params=par, data=body, verify=v)
            return re.text
        except Exception as e:
            print(e)
